package com.ants.boot.system.service.impl;

import com.ants.boot.system.dao.SysUserRoleMapper;
import com.ants.boot.system.domain.SysUserRole;
import com.ants.boot.system.service.SysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 蚂蚁会花呗
 * @date 2022/3/17 15:41
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Override
    public List<Integer> listRoleByUserId(Integer userId) {
        List<Integer> ids = new ArrayList<>();
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysUserRole::getUserId,userId);
        List<SysUserRole> list = this.list(queryWrapper);
        if (!list.isEmpty()){
            ids = list.stream().map(SysUserRole::getRoleId).collect(Collectors.toList());
        }
        return ids;
    }

    @Override
    public boolean delSysUserRole(Integer userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(SysUserRole::getUserId,userId);
        return this.remove(queryWrapper);
    }

    @Override
    public boolean saveSysUserRoleBatch(List<SysUserRole> list) {
        return this.saveBatch(list);
    }
}
